/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package isd.general.dao;

import isd.general.modelo.GR_Moneda;
import isd.general.modelo.GR_MonedaValores;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.Query;

/**
 *
 * @author ctrosch
 */
@Stateless
@LocalBean
public class MonedaDAO extends BaseDAO {
    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    public GR_Moneda getMoneda(String codcof){

        return getObjeto(GR_Moneda.class, codcof);
    }

    public List<GR_Moneda> getLista(){
        String sQuery = "SELECT m FROM Moneda m WHERE m.codcof = 'USD' OR m.codcof = 'ARS'";
        return queryList(GR_Moneda.class, sQuery);
    }


    public GR_MonedaValores getCotizacionDia(String codcof){

        try {
            String sQuery = "SELECT m FROM MonedaValores m WHERE m.id.codcof = '" + codcof + "' "
                    + "ORDER BY m.feccal desc";

            Query q = em.createQuery(sQuery);
            q.setFirstResult(1);
            q.setMaxResults(1);
            return (GR_MonedaValores) q.getSingleResult();
        } catch (Exception e) {

            System.err.println("Error al obtener cotizacion del día");
            return new GR_MonedaValores();
        }

    }


 
}
